package com.obor.quartz.job;

import java.util.Objects;

import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.SchedulerContext;
import org.quartz.SchedulerException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.zn.mvcbase.rest.RestEntityFactroy;

/**
* 
* @Company OBOR
* @Author 周楠
* @Description
* 2019年4月3日
*
*/
public abstract class QuartzJob implements Job{

	protected JobExecutionContext ex_ctx;
	
	protected SchedulerContext sch_ctx;
	protected Logger log=LoggerFactory.getLogger(QuartzJob.class);
	protected JobDataMap  dataMap;
	
	
	protected RestEntityFactroy ref;
	
	@Override
	public void execute(JobExecutionContext context) throws JobExecutionException {
		// TODO Auto-generated method stub
		
		log.debug("{} execute",context.getJobDetail().getKey().toString());
		
		dataMap =context.getJobDetail().getJobDataMap();
		
		if(Objects.isNull(ex_ctx)) ex_ctx=context;
		
		if(Objects.isNull(sch_ctx)) {
		try {
			sch_ctx=context.getScheduler().getContext();
			ref=(RestEntityFactroy) sch_ctx.get("REF");
		} catch (SchedulerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		}
		
		try {
			doJob();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			
			throw new JobExecutionException();
			
		}

		
	}
	
	
	public abstract void doJob()  throws Exception;
	
	
	
}
